package com.yanqu.road.dao.impl.activity.peakseacraft;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.peakseacraft.PeakSeacraftCrossUserData;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class PeakSeacraftCrossUserDataDaoImpl extends TempDataObjectDao<PeakSeacraftCrossUserData> {
    public static String getPeakSeacraftCrossUserDataSql = "select * from `t_c_activity_peak_seacraft_user` where `activityId`=? and unionGroup=?";

    public Map<Long, PeakSeacraftCrossUserData> getPeakSeacraftCrossUserDataMap(int activityId, int unionGroup) {
        Map<Long, PeakSeacraftCrossUserData> dataMap = new ConcurrentHashMap<>();
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getCross2Db());
            call = getDb().getConnection().prepareCall(getPeakSeacraftCrossUserDataSql);
            int i = 1;
            call.setInt(i++, activityId);
            call.setInt(i++, unionGroup);
            rs = call.executeQuery();
            while (rs.next()) {
                PeakSeacraftCrossUserData data = parse(rs);
                dataMap.put(data.getUserId(), data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return dataMap;
    }


    public static String updatePeakSeacraftCrossUserDataSql = "update `t_c_activity_peak_seacraft_user` set serverId=?,unionId=?,unionPosition=?,score=?,raidAttackTimes=?,round=?,attackNum=?,shipMemberMap=?,donateExp=?,useSpeedUpInfoMap=?,roundScore=? where activityId=? and unionGroup=? and userId=?";
    @Override
    protected boolean updateData(PeakSeacraftCrossUserData peakSeacraftCrossUserData) {
        peakSeacraftCrossUserData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCross2Db());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.BIGINT, peakSeacraftCrossUserData.getServerId()));
        para.put(i++, new DbParameter(Types.VARCHAR, peakSeacraftCrossUserData.getUnionId()));
        para.put(i++, new DbParameter(Types.INTEGER, peakSeacraftCrossUserData.getUnionPosition()));
        para.put(i++, new DbParameter(Types.INTEGER, peakSeacraftCrossUserData.getScore()));
        para.put(i++, new DbParameter(Types.INTEGER, peakSeacraftCrossUserData.getRaidAttackTimes()));
        para.put(i++, new DbParameter(Types.INTEGER, peakSeacraftCrossUserData.getRound()));
        para.put(i++, new DbParameter(Types.INTEGER, peakSeacraftCrossUserData.getAttackNum()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(peakSeacraftCrossUserData.getShipMemberMap())));
        para.put(i++, new DbParameter(Types.INTEGER, peakSeacraftCrossUserData.getDonateExp()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(peakSeacraftCrossUserData.getUseSpeedUpInfoMap())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(peakSeacraftCrossUserData.getRoundScore())));
        para.put(i++, new DbParameter(Types.INTEGER, peakSeacraftCrossUserData.getActivityId()));
        para.put(i++, new DbParameter(Types.INTEGER, peakSeacraftCrossUserData.getUnionGroup()));
        para.put(i++, new DbParameter(Types.BIGINT, peakSeacraftCrossUserData.getUserId()));
        if (getDb().executeNoneQuery(updatePeakSeacraftCrossUserDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertPeakSeacraftCrossUserDataSql = "insert into `t_c_activity_peak_seacraft_user`(activityId,unionGroup,userId,serverId,unionId,unionPosition,score,raidAttackTimes,round,attackNum,shipMemberMap,donateExp,useSpeedUpInfoMap,roundScore) values (?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
    @Override
    protected boolean addData(PeakSeacraftCrossUserData peakSeacraftCrossUserData) {
        peakSeacraftCrossUserData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCross2Db());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, peakSeacraftCrossUserData.getActivityId()));
        para.put(i++, new DbParameter(Types.INTEGER, peakSeacraftCrossUserData.getUnionGroup()));
        para.put(i++, new DbParameter(Types.BIGINT, peakSeacraftCrossUserData.getUserId()));
        para.put(i++, new DbParameter(Types.BIGINT, peakSeacraftCrossUserData.getServerId()));
        para.put(i++, new DbParameter(Types.VARCHAR, peakSeacraftCrossUserData.getUnionId()));
        para.put(i++, new DbParameter(Types.INTEGER, peakSeacraftCrossUserData.getUnionPosition()));
        para.put(i++, new DbParameter(Types.INTEGER, peakSeacraftCrossUserData.getScore()));
        para.put(i++, new DbParameter(Types.INTEGER, peakSeacraftCrossUserData.getRaidAttackTimes()));
        para.put(i++, new DbParameter(Types.INTEGER, peakSeacraftCrossUserData.getRound()));
        para.put(i++, new DbParameter(Types.INTEGER, peakSeacraftCrossUserData.getAttackNum()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(peakSeacraftCrossUserData.getShipMemberMap())));
        para.put(i++, new DbParameter(Types.INTEGER, peakSeacraftCrossUserData.getDonateExp()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(peakSeacraftCrossUserData.getUseSpeedUpInfoMap())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(peakSeacraftCrossUserData.getRoundScore())));
        if (getDb().executeNoneQuery(insertPeakSeacraftCrossUserDataSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected PeakSeacraftCrossUserData parseData(ResultSet rs) throws SQLException {
        PeakSeacraftCrossUserData peakSeacraftCrossUserData = new PeakSeacraftCrossUserData();
        peakSeacraftCrossUserData.setActivityId(rs.getInt("activityId"));
        peakSeacraftCrossUserData.setUnionGroup(rs.getInt("unionGroup"));
        peakSeacraftCrossUserData.setUserId(rs.getLong("userId"));
        peakSeacraftCrossUserData.setServerId(rs.getLong("serverId"));
        peakSeacraftCrossUserData.setUnionId(rs.getString("unionId"));
        peakSeacraftCrossUserData.setUnionPosition(rs.getInt("unionPosition"));
        peakSeacraftCrossUserData.setScore(rs.getInt("score"));
        peakSeacraftCrossUserData.setRaidAttackTimes(rs.getInt("raidAttackTimes"));
        peakSeacraftCrossUserData.setRound(rs.getInt("round"));
        peakSeacraftCrossUserData.setAttackNum(rs.getInt("attackNum"));
        peakSeacraftCrossUserData.setShipMemberMap(JSON.parseObject(rs.getString("shipMemberMap"), new TypeReference<ConcurrentHashMap<java.lang.Integer, com.yanqu.road.entity.activity.peakseacraft.PeakSeacraftTeamInfo>>() {}));
        peakSeacraftCrossUserData.setDonateExp(rs.getInt("donateExp"));
        peakSeacraftCrossUserData.setUseSpeedUpInfoMap(JSON.parseObject(rs.getString("useSpeedUpInfoMap"), new TypeReference<ConcurrentHashMap<java.lang.Integer, com.yanqu.road.entity.activity.peakseacraft.PeakSeacraftUseSpeedUpInfo>>() {}));
        peakSeacraftCrossUserData.setRoundScore(JSON.parseObject(rs.getString("roundScore"), new TypeReference<Map<Integer, Integer>>(){}));

        return peakSeacraftCrossUserData;
    }

}